<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI文生图 - 智能图像生成</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .container {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 800px;
            padding: 30px;
            text-align: center;
        }

        .header {
            margin-bottom: 30px;
        }

        .header h1 {
            font-size: 28px;
            font-weight: 700;
            color: #333;
            margin-bottom: 10px;
        }

        .header p {
            font-size: 16px;
            color: #666;
        }

        .input-group {
            margin-bottom: 25px;
        }

        .prompt-input {
            width: 100%;
            padding: 15px 20px;
            border: 2px solid #e0e0e0;
            border-radius: 12px;
            font-size: 16px;
            outline: none;
            transition: all 0.3s ease;
            resize: vertical;
            min-height: 100px;
        }

        .prompt-input:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .generate-btn {
            padding: 15px 30px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 12px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 10px;
        }

        .generate-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }

        .generate-btn:active {
            transform: translateY(0);
        }

        .generate-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .loading {
            display: none;
            margin: 30px 0;
            align-items: center;
            justify-content: center;
            gap: 15px;
        }

        .spinner {
            width: 30px;
            height: 30px;
            border: 3px solid rgba(102, 126, 234, 0.3);
            border-top: 3px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .loading-text {
            font-size: 18px;
            color: #666;
        }

        .image-container {
            margin-top: 30px;
            display: none;
        }

        .image-container h3 {
            margin-bottom: 15px;
            color: #333;
            font-size: 20px;
        }

        .generated-image {
            max-width: 100%;
            border-radius: 12px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            margin: 0 auto;
            display: block;
        }

        .error-message {
            display: none;
            background: #ffebee;
            color: #c62828;
            padding: 15px;
            border-radius: 10px;
            margin: 20px 0;
            animation: shake 0.5s ease-in-out;
        }

        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            25% { transform: translateX(-10px); }
            75% { transform: translateX(10px); }
        }

        .examples {
            margin-top: 30px;
            text-align: left;
        }

        .examples h3 {
            margin-bottom: 15px;
            color: #333;
        }

        .example-list {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 15px;
        }

        .example-item {
            background: #f8f9fa;
            padding: 12px;
            border-radius: 8px;
            font-size: 14px;
            cursor: pointer;
            transition: all 0.2s ease;
            border: 1px solid #eee;
        }

        .example-item:hover {
            background: #e9ecef;
            transform: translateY(-2px);
        }

        @media (max-width: 600px) {
            .container {
                padding: 20px;
            }

            .header h1 {
                font-size: 24px;
            }

            .example-list {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>🎨 AI文生图</h1>
        <p>输入文字描述，AI为您生成精美图像</p>
    </div>

    <div class="input-group">
            <textarea
                    class="prompt-input"
                    id="promptInput"
                    placeholder="请输入您想要生成的图像描述，例如：一只坐在草地上的可爱猫咪，夕阳西下..."
            ></textarea>
    </div>

    <button class="generate-btn" id="generateBtn">
        <span>生成图像</span>
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <path d="M5 12h14M12 5l7 7-7 7"></path>
        </svg>
    </button>

    <div class="error-message" id="errorMessage"></div>

    <div class="loading" id="loading">
        <div class="spinner"></div>
        <div class="loading-text">正在生成图像，请稍候...</div>
    </div>

    <div class="image-container" id="imageContainer">
        <h3>生成结果</h3>
        <img class="generated-image" id="generatedImage" alt="生成的图像">
    </div>

    <div class="examples">
        <h3>示例提示词</h3>
        <div class="example-list" id="exampleList">
            <div class="example-item" data-prompt="一只坐在草地上的可爱猫咪，夕阳西下">可爱猫咪在草地</div>
            <div class="example-item" data-prompt="未来城市的夜景，霓虹灯闪烁，赛博朋克风格">未来城市夜景</div>
            <div class="example-item" data-prompt="山水画风格的江南小镇，小桥流水人家">江南水乡</div>
            <div class="example-item" data-prompt="科幻太空站内部，高科技设备，宇航员">太空站内部</div>
            <div class="example-item" data-prompt="梦幻森林中的独角兽，魔法光芒">独角兽与魔法森林</div>
            <div class="example-item" data-prompt="抽象艺术风格的音乐符号，色彩斑斓">抽象音乐艺术</div>
        </div>
    </div>
</div>

<script>
    class ImageGenerator {
        constructor() {
            this.promptInput = document.getElementById('promptInput');
            this.generateBtn = document.getElementById('generateBtn');
            this.loading = document.getElementById('loading');
            this.errorMessage = document.getElementById('errorMessage');
            this.imageContainer = document.getElementById('imageContainer');
            this.generatedImage = document.getElementById('generatedImage');
            this.exampleList = document.getElementById('exampleList');

            this.initEventListeners();
        }

        initEventListeners() {
            this.generateBtn.addEventListener('click', () => this.generateImage());
            this.promptInput.addEventListener('keypress', (e) => {
                if (e.ctrlKey && e.key === 'Enter') {
                    this.generateImage();
                }
            });

            // 为示例添加点击事件
            document.querySelectorAll('.example-item').forEach(item => {
                item.addEventListener('click', () => {
                    this.promptInput.value = item.getAttribute('data-prompt');
                });
            });
        }

        async generateImage() {
            const prompt = this.promptInput.value.trim();

            if (!prompt) {
                this.showError('请输入图像描述');
                return;
            }

            // 显示加载状态
            this.showLoading();
            this.hideError();
            this.hideImage();

            try {
                // 调用后端接口生成图像
                const response = await fetch(`/image?input=${encodeURIComponent(prompt)}`);

                if (response.ok) {
                    // 获取重定向的URL
                    const imageUrl = response.url;

                    // 显示生成的图像
                    this.showImage(imageUrl);
                } else {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
            } catch (error) {
                console.error('Error:', error);
                this.showError('图像生成失败，请稍后重试');
            } finally {
                this.hideLoading();
                this.generateBtn.disabled = false;
            }
        }

        showLoading() {
            this.loading.style.display = 'flex';
            this.generateBtn.disabled = true;
        }

        hideLoading() {
            this.loading.style.display = 'none';
        }

        showError(message) {
            this.errorMessage.textContent = message;
            this.errorMessage.style.display = 'block';
        }

        hideError() {
            this.errorMessage.style.display = 'none';
        }

        showImage(imageUrl) {
            this.generatedImage.src = imageUrl;
            this.imageContainer.style.display = 'block';

            // 滚动到图像位置
            this.imageContainer.scrollIntoView({ behavior: 'smooth' });
        }

        hideImage() {
            this.imageContainer.style.display = 'none';
        }
    }

    // 初始化应用
    document.addEventListener('DOMContentLoaded', () => {
        new ImageGenerator();
    });
</script>
</body>
</html>
